<?php

namespace Plugins\AdminBasic\Http\Admin\Controller;

use App\Http\AbstractController;
use App\Middlewares\Admin\AuthenticateMiddleware;
use App\Middlewares\Admin\PermissionsMiddleware;
use App\Util\Response;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\GetMapping;
use Hyperf\HttpServer\Annotation\Middlewares;
use Hyperf\View\Render;
use Psr\Http\Message\ResponseInterface;
use Plugins\AdminBasic\Http\Admin\Service\RouteService;
use Hyperf\HttpServer\Annotation\PostMapping;
use Hyperf\Validation\Annotation\Scene;
use App\Exception\ApiMessageException;
use Hyperf\Validation\Middleware\ValidationMiddleware;
use Plugins\AdminBasic\Requests\BasicRouteRequest;
use Psr\SimpleCache\InvalidArgumentException;


/**
 * 路由菜单
 */
#[Controller(prefix: 'admin/plugins/admin-basic/route')]
#[Middlewares([AuthenticateMiddleware::class, PermissionsMiddleware::class])]
class RouteController extends AbstractController
{


    /**
     * 列表页
     * 
     * @param Render $render
     * 
     * @return ResponseInterface
     */
    #[GetMapping(path: 'lists')]
    public function lists(Render $render): ResponseInterface
    {
        return $render->render('plugins.AdminBasic.Admin.Route.lists');
    }

    /**
     * 列表数据
     * 
     * @return ResponseInterface
     */
    #[GetMapping(path: 'lists-data')]
    public function listsData(): ResponseInterface
    {
        return Response::json()->success(RouteService::aop()->lists());
    }

    /**
     * 创建数据
     * 
     * @param BasicRouteRequest $request
     * 
     * @return ResponseInterface
     * @throws ApiMessageException
     * @throws InvalidArgumentException
     */
    #[Scene('store')]
    #[PostMapping(path: 'create')]
    #[Middlewares([ValidationMiddleware::class])]
    public function store(BasicRouteRequest $request): ResponseInterface
    {
        RouteService::aop()->store($request->post());
        return Response::json()->success();
    }

    /**
     * 更新数据
     * 
     * @param BasicRouteRequest $request
     * 
     * @return ResponseInterface
     * @throws ApiMessageException
     * @throws InvalidArgumentException
     */
    #[Scene('update')]
    #[PostMapping(path: 'update')]
    #[Middlewares([ValidationMiddleware::class])]
    public function update(BasicRouteRequest $request): ResponseInterface
    {
        RouteService::aop()->update($request->post());
        return Response::json()->success();
    }

    /**
     * 删除数据
     * 
     * @param BasicRouteRequest $request
     * 
     * @return ResponseInterface
     * @throws ApiMessageException
     */
    #[Scene('destroy')]
    #[PostMapping(path: 'destroy')]
    #[Middlewares([ValidationMiddleware::class])]
    public function destroy(BasicRouteRequest $request): ResponseInterface
    {
        RouteService::aop()->destroy($request->post('ids'));
        return Response::json()->success();
    }

    /**
     * 排序
     *
     * @return ResponseInterface
     * @throws ApiMessageException
     */
    #[PostMapping('sort')]
    public function sort(): ResponseInterface
    {
        $res = RouteService::aop()->sort(
            (int)$this->request->post('oldId'),
            (int)$this->request->post('newId'),
            (int)$this->request->post('isUp'),
        );

        return Response::json()->success($res);
    }
}